

package TransportesMarroquin;



import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.File;
import javax.sound.sampled.AudioSystem;
import javax.swing.JOptionPane;
public class Cliente extends javax.swing.JFrame {

  ResultSetTableModel tableModel;
  String consulta = "SELECT * FROM cliente";
  String nombre = new String ();
  String apellido = new String ();
  
  boolean borrar = false ;
  int codigo;
  String nombres,apellidos; // variables de la seleccion  
    int row;
    boolean entro = false;
   javax.sound.sampled.Clip sonido;
    private String file;
 
   public Cliente()  {
        initComponents();

        
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lnum2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jButton8 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cbus1 = new javax.swing.JTextField();
        cbus = new javax.swing.JTextField();
        jButton7 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        trash = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(143, 0, 0));

        lnum2.setFont(new java.awt.Font("Tahoma", 1, 18));
        lnum2.setForeground(new java.awt.Color(255, 255, 255));
        lnum2.setText("Clientes");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lnum2)
                .addContainerGap(539, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lnum2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        try{
            tableModel = new ResultSetTableModel( consulta );
        }catch(Exception e){
            System.out.println("ERROR");
        }
        resultTable.setModel(tableModel);
        resultTable.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        resultTable.setSelectionBackground(new java.awt.Color(255, 255, 255));
        resultTable.setSelectionForeground(new java.awt.Color(0, 0, 0));
        resultTable.getTableHeader().setReorderingAllowed(false);
        resultTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                resultTableMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                resultTableMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                resultTableMouseReleased(evt);
            }
        });
        resultTable.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                resultTableMouseDragged(evt);
            }
        });
        jScrollPane1.setViewportView(resultTable);

        jButton8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/1316277158_filenew.png"))); // NOI18N
        jButton8.setText("Nuevo");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/regresar.png"))); // NOI18N
        jButton3.setText("Regresar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/1316367384_search.png"))); // NOI18N

        cbus1.setText("Buscar por Apellido");
        cbus1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cbus1MouseClicked(evt);
            }
        });
        cbus1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cbus1KeyReleased(evt);
            }
        });

        cbus.setText("Buscar por Nombre");
        cbus.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cbusMouseClicked(evt);
            }
        });
        cbus.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cbusKeyReleased(evt);
            }
        });

        jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/1316298596_print_16x16.gif"))); // NOI18N
        jButton7.setText("Imprimir");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/1316393209_mail_new.png"))); // NOI18N
        jButton9.setText("Correo");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        trash.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/empty.png"))); // NOI18N
        trash.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                trashMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                trashMouseExited(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                trashMouseReleased(evt);
            }
        });
        trash.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                trashMouseMoved(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(120, 120, 120)
                .addComponent(cbus, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cbus1, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(jLabel1)
                .addGap(64, 64, 64))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(trash, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbus1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 105, Short.MAX_VALUE)
                        .addComponent(trash, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void resultTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resultTableMouseClicked
         int fila = resultTable.rowAtPoint(evt.getPoint());
                if (fila > -1)
                {
                      dispose();
                      Object objeto = tableModel.getValueAt(fila, 0);

                     String ja = objeto.toString();
                     int num=Integer.parseInt(ja);


                      ClienteSelect va = new ClienteSelect (num);
                      va.setVisible(true);
                      va.setResizable(false);

                }
}//GEN-LAST:event_resultTableMouseClicked

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        dispose();
        NuevoCliente va = new NuevoCliente();
        va.setVisible(true);
}//GEN-LAST:event_jButton8ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        dispose();
}//GEN-LAST:event_jButton3ActionPerformed

    private void cbus1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cbus1MouseClicked
        cbus1.setText("");
}//GEN-LAST:event_cbus1MouseClicked

    private void cbus1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cbus1KeyReleased
        
        
        apellido = cbus1.getText();
        
        
        consulta = "SELECT * FROM cliente where nombre like '"+nombre+"%' AND apellido like '"+apellido+"%'";
        try{
            
            tableModel = new ResultSetTableModel( consulta );
        } catch ( Exception e) {
            JOptionPane.showMessageDialog( null,
                    e );}
        resultTable.setModel(tableModel);
}//GEN-LAST:event_cbus1KeyReleased

    private void cbusMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cbusMouseClicked
        cbus.setText("");        // TODO add your handling code here:
}//GEN-LAST:event_cbusMouseClicked

    private void cbusKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cbusKeyReleased
        
        
        
        nombre = cbus.getText();
        
        
        consulta = "SELECT * FROM cliente where nombre like '"+nombre+"%' AND apellido like '"+apellido+"%'";
        try{
            
            tableModel = new ResultSetTableModel( consulta );
        } catch ( Exception e) {
            JOptionPane.showMessageDialog( null,
                    e );}
        resultTable.setModel(tableModel);
}//GEN-LAST:event_cbusKeyReleased

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        
        file=Archivo.TipoA("ListadoCliente",nombre,apellido);
        Archivo.Abrir(file);
        
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        file=Archivo.TipoA("ListadoCliente",nombre,apellido);
        Correo.EnviarArchivo(Inicio.usuario,file);
}//GEN-LAST:event_jButton9ActionPerformed

    private void trashMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_trashMouseEntered
        if(borrar == true){
            entro = true;
            trash.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/full.png")));
        }
}//GEN-LAST:event_trashMouseEntered

    private void trashMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_trashMouseExited
        entro= false;
        borrar = false;
        trash.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/empty.png")));
}//GEN-LAST:event_trashMouseExited

    private void trashMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_trashMouseReleased
        
}//GEN-LAST:event_trashMouseReleased

    private void trashMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_trashMouseMoved
        
    }//GEN-LAST:event_trashMouseMoved

    private void resultTableMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resultTableMouseDragged
         if(borrar == true && entro == false){
          Toolkit tk = Toolkit.getDefaultToolkit();
                Image imagen = tk.getImage(System.getProperty ("user.dir")+"\\src\\Imagenes\\basura.png");     
                Point hotSpot = new Point(0,0);  
                Cursor cursor = tk.createCustomCursor(imagen, hotSpot, "basura");
                this.setCursor(cursor);
      }        // TODO add your handling code here:
    }//GEN-LAST:event_resultTableMouseDragged

    private void resultTableMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resultTableMousePressed
       row = resultTable.rowAtPoint(evt.getPoint());
    
      borrar=true;
     
      // accion qee va hacer siempre 
      if (row > -1)
                {
                      
                      Object objeto = tableModel.getValueAt(row, 0);

                     String ja = objeto.toString();
                     // leo el nombre
                     objeto = tableModel.getValueAt(row, 1);
                     nombres = objeto.toString();
                     
                     // leo el apellido 
                     objeto = tableModel.getValueAt(row, 2);
                     apellidos = objeto.toString();
                     
                     codigo=Integer.parseInt(ja);

                }     
      
    }//GEN-LAST:event_resultTableMousePressed

    private void resultTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resultTableMouseReleased
      // accion del release 
        if(borrar == true && entro == true){
                  try {
                sonido = null;
                sonido = AudioSystem.getClip();
                sonido.open(AudioSystem.getAudioInputStream(new File(System.getProperty("user.dir") + "\\src\\SonidosPractica\\basura.wav")));
                sonido.start();




            } catch (Exception e) {
                System.out.println("" + e);
            } // fin

               
                int messageType = JOptionPane.QUESTION_MESSAGE;
                String[] options = {"Continuar", "Cancelar"};
                int code = JOptionPane.showInternalOptionDialog(this.getContentPane(),
                        "¿Desea realmente borrar a "+nombres+" "+apellidos+"?", 
                         "Confirmación", 1, messageType, 
                         null, options, "Cancelar");
               
                    
               
               
               if(code==0){
                    Error.Mysql("Delete from cliente where idcliente = "+codigo);
               // reconfiguro todo 
               consulta = "SELECT * FROM cliente";
                      try{

                tableModel = new ResultSetTableModel( consulta );
                }
                catch ( Exception e) {
                 JOptionPane.showMessageDialog( null,
                 e );}
                      resultTable.setModel(tableModel);
                      // sonido 
                      
        
                          }
               else
                   if(code == 1){
                       
                   }
               borrar = false;
               entro=false;
               }
               
     trash.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/empty.png")));         
     borrar = false;
    }//GEN-LAST:event_resultTableMouseReleased

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               
                    new Cliente().setVisible(true);
               
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField cbus;
    private javax.swing.JTextField cbus1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lnum2;
    private final javax.swing.JTable resultTable = new javax.swing.JTable(tableModel);
    private javax.swing.JLabel trash;
    // End of variables declaration//GEN-END:variables

}
